package cn.zhouchao.pet.service.impl;

import cn.zhouchao.basic.util.BaiduAuditUtils;
import cn.zhouchao.basic.util.LoginContext;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.mapper.EmployeeMapper;
import cn.zhouchao.pet.domain.Pet;
import cn.zhouchao.pet.domain.PetDetail;
import cn.zhouchao.pet.domain.PetOnlineAuditLog;
import cn.zhouchao.pet.mapper.PetDetailMapper;
import cn.zhouchao.pet.mapper.PetMapper;
import cn.zhouchao.pet.mapper.PetOnlineAuditLogMapper;
import cn.zhouchao.pet.service.IPetService;
import cn.zhouchao.basic.service.impl.BaseServiceImpl;
import cn.zhouchao.user.domain.User;
import cn.zhouchao.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhouchao
 * @since 2023-03-13
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    PetOnlineAuditLogMapper logMapper;

    @Override
    public void add(Pet pet) {
        pet.setCreatetime(new Date());
        pet.setState(0);
        Long id = LoginContext.getLogininfo().getId();
        Employee emp = employeeMapper.findByLogininfoId(id);
        pet.setShopId(emp.getShopId());

        User user = userMapper.findByLogininfoId(id);
        pet.setUserId(user.getId());
        super.add(pet);
        PetDetail petDetail = pet.getDetail();
        if (null!=petDetail){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        petDetailMapper.update(pet.getDetail());
    }

    @Override
    public void onsale(List<Long> ids) {
        // 参数校验
        // 判断是否是上架 如果是上架 就不管 我们只管下架状态的
        List<Pet> pets =  petMapper.findByonsaleIds(ids);
        if (CollectionUtils.isEmpty(pets)){
            // 判断当前集合是否为空
            return;
        }
        // 百度AI审核
        List<PetOnlineAuditLog> logList = new ArrayList<>();
        List<Long> petIds = new ArrayList<>();
        pets.forEach(pet->{
            StringBuilder sb = new StringBuilder();
            // 名称
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sb.append("宠物名称审核不通过！宠物id为").append(pet.getId()).append(";");
            }
            // 图片
            String resources = pet.getResources();
            if (StringUtils.isNotBlank(resources)){
                String[] split = resources.split(",");
                for (int i = 0; i < split.length; i++) {
                    if (!BaiduAuditUtils.ImgCensor(split[i])){
                        sb.append("宠物图片审核不通过！宠物id为").append(pet.getId());
                    }

                }
            }
            // 去添加日志
            if (sb.length()>0){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditId(-1L);
                log.setNote(sb.toString());
                log.setAuditTime(new Date());
                log.setPetId(pet.getId());
                log.setState(0);
//                logMapper.add(log);
                logList.add(log);
                return;
            }
            // 修改pet状态
//            pet.setState(1);
//            pet.setOnsaletime(new Date());
//            petMapper.update(pet);
              petIds.add(pet.getId());
        });
        if (!CollectionUtils.isEmpty(petIds)){
            petMapper.onsale(petIds);
        }
        // 添加日志--只有审核不通过才添加
        if (!CollectionUtils.isEmpty(logList)){
            // 批量添加
            logMapper.batchAdd(logList);
        }

    }

    @Override
    public void offsale(List<Long> ids) {
        // 判断是否是上架 如果是上架 就不管 我们只管下架状态的
        List<Pet> pets =  petMapper.findByoffsaleIds(ids);
        if (CollectionUtils.isEmpty(pets)){
            // 判断当前集合是否为空
            return;
        }
        List<PetOnlineAuditLog> logList = new ArrayList<>();
        List<Long> petIds = new ArrayList<>();
        pets.forEach(pet->{
            StringBuilder sb = new StringBuilder();
            // 去添加日志
            if (sb.length()>0){
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setAuditId(-1L);
                log.setNote(sb.toString());
                log.setAuditTime(new Date());
                log.setPetId(pet.getId());
                log.setState(0);
//                logMapper.add(log);
                logList.add(log);
                return;
            }
            // 修改pet状态
//            pet.setState(1);
//            pet.setOnsaletime(new Date());
//            petMapper.update(pet);
            petIds.add(pet.getId());
        });
        if (!CollectionUtils.isEmpty(petIds)){
            petMapper.offsale(petIds);
        }
        // 添加日志--只有审核不通过才添加
        if (!CollectionUtils.isEmpty(logList)){
            // 批量添加
            logMapper.batchAdd(logList);
        }
        petMapper.offsale(ids);
    }
}
